package com.wmz.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmz.common.utils.PageUtils;
import com.wmz.common.utils.Query;

import com.wmz.gulimall.product.dao.CategoryDao;
import com.wmz.gulimall.product.entity.CategoryEntity;
import com.wmz.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        // 获取所有的商品
        List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(null);
        // 获取根节点
        List<CategoryEntity> categoryRoot = categoryEntityList.stream()
                .filter(entity -> entity.getParentCid().equals(0L))
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChildren(categoryEntity,categoryEntityList));
                    return categoryEntity;
                })
                .sorted(Comparator.comparing(CategoryEntity::getSort))
                .collect(Collectors.toList());
        return categoryRoot;
    }

    @Override
    public void deleteCategoryByCatIds(Long[] catIds) {
        // 判断该节点是否被其他所引用
        // 未引用就删除
        this.baseMapper.deleteBatchIds(Arrays.asList(catIds));
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        findParentPath(catelogId,paths);
        return paths.toArray(new Long[paths.size()]);
    }

    /**
     * 递归查找所有父categoryId
     * @param catelogId 当前的ID
     * @param paths 所有ID的集合
     */
    private void findParentPath(Long catelogId,List<Long> paths) {
        CategoryEntity category = this.getById(catelogId);
        // 如果父ID不为0，则开始递归查找
        if(category.getParentCid() != 0) {
            findParentPath(category.getParentCid(),paths);
        }
        paths.add(catelogId);
    }

    /**
     * 根据父节点来查询所有子节点
     * @param root  父节点
     * @param categoryEntityList 所有节点
     * @return
     */
    public List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> categoryEntityList){
        // 设置所有下级目录
        List<CategoryEntity> categoryChildren = categoryEntityList.stream()
                .filter(entity -> entity.getParentCid().equals(root.getCatId()))  //过滤出所有entity父ID等于root种类ID的目录
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChildren(categoryEntity,categoryEntityList));
                    return categoryEntity;
                })
                .sorted(Comparator.comparing(CategoryEntity::getSort))            // 进行排序
                .collect(Collectors.toList());
        return categoryChildren;
    }

}
